package xj.graph2d;

import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;

public class Group extends CompoundDrawShape {

  public Group() {
    this(false);
  }

  public Group(boolean temporary) {
    this.temporary = temporary;
    fitted = false;
    active = true;
  }

  @Override
  public DrawShape makeInstance() {
    return new Group();
  }

  @Override
  public void copyTo(DrawShapeBase s) 
    throws CloneNotSupportedException {
    super.copyTo(s);
    if (s instanceof Group) {
      Group g = (Group) s;
      g.active = active;
      g.drawGroupFrame = drawGroupFrame;
      g.groupFrameColor = groupFrameColor;
      g.temporary = temporary;
    }
  }

  public boolean isActive() {
    return active;
  }

  public void activate() {
    Iterator iter = subshapes.iterator();
    while (iter.hasNext()) {
      DrawShape s = (DrawShape) iter.next();
      s.setParent(this);
      s.setSearchable(false);
    }
    active = true;
  }

  public void deactivate() {
    active = false;
  }

  public boolean isTemporary() {
    return temporary;
  }

  public void setTemporary(boolean temporary) {
    this.temporary = temporary;
  }

  public boolean contains(DrawShape s) {
    if (s != null) {
      Rectangle2D r = s.getBounds();
      double x = r.getX();
      double y = r.getY();
      double w = r.getWidth();
      double h = r.getHeight();
      if (w != 0 && h != 0) {
	return bounds.contains(x, y, w, h);
      } else {
	return (bounds.contains(x, y) && bounds.contains(x + w, y + h));
      }
    }
    return false;
  }

  protected boolean active = true;

  protected boolean temporary = false;

  protected boolean drawGroupFrame = true;

  protected Color groupFrameColor = new Color(255, 200, 0, 128);

}
